<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="https://unpkg.com/@babel/standalone@7.8.7/babel.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.production.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat" style="position: relative"></main>
    <script type="text/babel">
      run(async function () {
        const {
          React,
          ReactDOM: { render },
          WebChat: { ReactWebChat }
        } = window; // Imports in UMD fashion.

        const { directLine, store } = testHelpers.createDirectLineEmulator();

        const App = () => (
          <React.Fragment>
            <ReactWebChat directLine={directLine} store={store} />
            <div style={{ gap: 8, position: 'absolute', top: 0, width: '100%' }}>
              <label>
                <div>Plain text box</div>
                <input
                  data-testid="plain-text-box"
                  spellCheck={false}
                  style={{ background: '#f0f0f0', border: 0, height: 50, padding: 0, width: '100%' }}
                  type="textbox"
                />
              </label>
              <label>
                <div>Rich text box</div>
                <div
                  contentEditable={true}
                  data-testid="rich-text-box"
                  spellCheck={false}
                  style={{ background: '#f0f0f0', border: 0, height: 50, width: '100%' }}
                />
              </label>
            </div>
          </React.Fragment>
        );

        render(<App />, document.getElementById('webchat'));

        await pageConditions.uiConnected();

        expect(window.isSecureContext).toBe(true);

        await host.sendDevToolsCommand('Browser.setPermission', {
          permission: { name: 'clipboard-write' },
          setting: 'granted'
        });

        await expect(navigator.permissions.query({ name: 'clipboard-write' })).resolves.toHaveProperty(
          'state',
          'granted'
        );

        await directLine.emulateIncomingActivity({
          entities: [
            {
              '@context': 'https://schema.org',
              '@id': '',
              '@type': 'Message',
              keywords: ['AllowCopy'],
              type: 'https://schema.org/Message'
            }
          ],
          text: 'Mollit *aute* **aute** dolor ea ex magna incididunt nostrud sit nisi. ![Icon]()',
          type: 'message'
        });

        await pageConditions.numActivitiesShown(1);

        // WHEN: Focus on the "Copy" button via keyboard.
        await host.click(document.querySelector(`[data-testid="${WebChat.testIds.sendBoxTextBox}"]`));
        await host.sendShiftTab(3);
        await host.sendKeys('ENTER');


        // axe-core is crazy about empty button name while swapping name
        const { checkAccessibility } = window
        window.checkAccessibility = async () => { };

        // THEN: Should focus on the "Copy" button
        const copyButton = document.querySelector(`[data-testid="${WebChat.testIds.copyButton}"]`);

        expect(document.activeElement).toBe(copyButton);
        await host.snapshot('local');

        // WHEN: Press ENTER on the "Copy" button.
        await host.sendKeys('ENTER');

        // THEN: The copy button should change to "Copied".
        await host.snapshot('local');

        // WHEN: Paste into plain text and rich text text box.
        await host.click(document.querySelector('[data-testid="plain-text-box"]'));
        await host.sendKeys('+CONTROL', 'v', '-CONTROL');

        await host.click(document.querySelector('[data-testid="rich-text-box"]'));
        await host.sendKeys('+CONTROL', 'v', '-CONTROL');

        await host.click(document.querySelector(`[data-testid="${WebChat.testIds.sendBoxTextBox}"]`));

        // Sleep for 1 second for the "Copied" text to go away.
        await testHelpers.sleep(500);

        window.checkAccessibility = checkAccessibility;

        // THEN: Plain text box should contains plain text, while rich text box should contains rich text.
        await host.snapshot('local');
      });
    </script>
  </body>
</html>
